﻿using System;
using System.Web.Mvc;
using System.Web.Routing;

namespace T4MvcJs.RoutesHandler
{
    public class UrlGenerator
    {
        private readonly UrlHelper _urlHelper;

        public UrlGenerator(RouteCollection routes)
        {
            _urlHelper = UrlHelperFactory.CreateUrlHelper(routes);
        }

        public string GenerateUrl(string area, string controller, string action, params string[] parameterNames)
        {
            var routeValues = new RouteValueDictionary();
            foreach (var parameterName in parameterNames)
            {
                routeValues[parameterName] = "{" + parameterName + "}";
            }
            routeValues.Add("Area", area ?? "");

            var url = _urlHelper.Action(action, controller, routeValues);
            return Uri.UnescapeDataString(url);
        }
    }
}